package cn.cxion.chat.app.view.popupview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.cxion.chat.R;
import cn.cxion.chat.app.bean.OptionBean;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.util.XpopoHelp;

public class OptionListPopupView extends BottomPopupView {

    List<OptionBean> list;
    @Bind(R.id.lv_hold)
    ListView lvHold;
    private Context context;
    private  OnOptionSelectListener listener;


    public static  void showView(Context context, List<OptionBean> list,OnOptionSelectListener listener){
        OptionListPopupView optionListPopupView=new OptionListPopupView(context,list,listener);
        XpopoHelp.showView(context,optionListPopupView);
    }


    public OptionListPopupView(@NonNull Context context, List<OptionBean> list,OnOptionSelectListener listener) {
        super(context);
        this.list = list;
        this.context=context;
        this.listener=listener;

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_option;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    //初始化界面
    private void initView() {
        ButterKnife.bind(this);
        lvHold.setAdapter(new MyAdapter());
        lvHold.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.ok(list.get(position));
                dismiss();
            }
        });
    }

    @Override
    protected void onShow() {
        super.onShow();


    }

    public interface OnOptionSelectListener{
        void ok(OptionBean optionBean);
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getWindowHeight(getContext()) * 0.75);
    }

    class MyAdapter extends BaseAdapter{


        @Override
        public int getCount() {
            return list==null?1:list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                convertView= LayoutInflater.from(context).inflate(R.layout.popu_item_option,null);
                holder=new ViewHolder();
                holder.tv=convertView.findViewById(R.id.tv);
                convertView.setTag(holder);
            }else{
                holder= (ViewHolder) convertView.getTag();
            }
            holder.tv.setText(list.get(position).getName());
            return convertView;
        }

    }
    class ViewHolder{
        TextView tv;
    }
}
